<!DOCTYPE HTML>
<html lang="zh">
<head>
<meta charset="utf-8" />
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="Txazo BlogSite">
<meta http-equiv="description" content="Txazo BlogSite">
<title>Index</title>
<link type="text/css" rel="stylesheet" href="../css/code.css" />
</head>
<body>
	<div id="nav">
		<div id="nav-top">
			<h3>Servlet过滤器</h3>
		</div>
		<div class="nav-list">
			<div class="nav-title">
				<span>Servlet过滤器</span>
			</div>
			<pre class="nav-text">
Servlet过滤器用于对Web应用程序的请求和响应执行统一的前期或后期过滤操作，从而拦截请求和响应，
或者对请求和响应信息执行必要的修改操作。
</pre>
			<pre class="nav-text">
Servlet过滤器的实质是在Filter的doFilter()方法中对ServletRequest和ServletResponse进
行处理，处理之前会转换为HttpServletRequest和HttpServletResponse，然后通过FilterChain
的doFilter()方法将请求和响应传递给Servlet或下一个过滤器。
</pre>
		</div>
		<div class="nav-list">
			<div class="nav-title">
				<span>Servlet过滤器</span>
			</div>
			<pre class="nav-text">
所有的Servlet过滤器类都必须实现Filter接口。
</pre>
			<pre class="nav-text">
自定义的Filter如下：
</pre>
			<pre class="nav-tab">
<span class="tab-title">属性             值              描述</span>
<hr class="sep" />
autoplay	autoplay	音频在就绪后马上播放
controls	controls	向用户显示控件，比如播放按钮
loop	        loop	        音频结束时重新开始播放
preload	        preload	        音频在页面加载时进行加载，并预备播放
src	        url	        要播放的音频的URL
</pre>
			<pre class="nav-java">
<span class="java-pln"><span class="java-kwd">public</span> <span
					class="java-kwd">class</span> GlobalFilter <span class="java-kwd">implements</span> Filter <span
					class="java-pun">{</span> 

    <span class="java-kwd">private</span> FilterConfig config <span
					class="java-pun">=</span> <span class="java-kwd">null</span>; 

    <span class="java-lit">@Override</span> 
    <span class="java-kwd">public</span> <span class="java-kwd">void</span> init<span
					class="java-pun">(</span>FilterConfig config<span class="java-pun">)</span> <span
					class="java-kwd">throws</span> ServletException <span
					class="java-pun">{</span> 
        <span class="java-kwd">this</span>.config <span class="java-pun">=</span> config; 
    <span class="java-pun">}</span> 

    <span class="java-lit">@Override</span> 
    <span class="java-kwd">public</span> <span class="java-kwd">void</span> doFilter<span
					class="java-pun">(</span>ServletRequest req, ServletResponse resp, 
            FilterChain chain<span class="java-pun">)</span> <span
					class="java-kwd">throws</span> IOException, ServletException <span
					class="java-pun">{</span> 
        HttpServletRequest request <span class="java-pun">=</span> <span
					class="java-pun">(</span>HttpServletRequest<span class="java-pun">)</span> req; 
        HttpServletResponse response <span class="java-pun">=</span> <span
					class="java-pun">(</span>HttpServletResponse<span class="java-pun">)</span> resp; 
        <span class="java-com">// 对request和response执行过滤前处理// 对request和response执行过滤前处理// 对request和response执行过滤前处理</span> 
        chain.doFilter<span class="java-pun">(</span>req, resp<span
					class="java-pun">)</span>; 
        <span class="java-com">// 对response执行过滤后处理</span> 
    <span class="java-pun">}</span> 

    <span class="java-lit">@Override</span> 
    <span class="java-kwd">public</span> <span class="java-kwd">void</span> destroy<span
					class="java-pun">(</span><span class="java-pun">)</span> <span
					class="java-pun">{</span> 
        <span class="java-kwd">this</span>.config <span class="java-pun">=</span> <span
					class="java-kwd">null</span>; 
    <span class="java-pun">}</span> 

<span class="java-pun">}</span></span>
</pre>
		</div>
		<div class="nav-list">
			<div class="nav-title">
				<span>Servlet过滤器</span>
			</div>
			<pre class="nav-text">
在web.xml中配置Servlet过滤器。
</pre>
			<pre class="nav-xml"
				onmouseover="this.style.overflowX='auto';document.getElementById('nav-hidden').style.display='none'"
				onmouseout="this.style.overflowX='hidden';document.getElementById('nav-hidden').style.display=''">
你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好好你好你好你好好你好你好你好
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
..........................................................................
22222222222222222222222222222222222222222222222222222222222222222222222222
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAA
A      A
1111
2222
8888
aaaa
iiii
wwww
llll
AAAA
WWWW
----
////
!!!!
{{{{
((((
[[[[
^^^^
<span class="xml-pln"><span class="xml-tag">&lt;filter&gt;</span>
    <span class="xml-tag">&lt;filter-name&gt;</span>GlobalFilter<span
					class="xml-tag">&lt;/filter-name&gt;</span>
    <span class="xml-tag">&lt;filter-class&gt;</span>com.filter.GlobalFilter<span
					class="xml-tag">&lt;/filter-class&gt;</span>
<span class="xml-tag">&lt;/filter&gt;</span>

<span class="xml-tag">&lt;filter-mapping&gt;</span>
    <span class="xml-tag">&lt;filter-name&gt;</span>GlobalFilter<span
					class="xml-tag">&lt;/filter-name&gt;</span>
    <span class="xml-tag">&lt;url-pattern&gt;</span><span
					class="xml-tag">/</span>*<span class="xml-tag">&lt;/url-pattern&gt;</span>
    <span class="xml-tag">&lt;dispatcher&gt;</span>REQUEST<span
					class="xml-tag">&lt;/dispatcher&gt;</span>
<span class="xml-tag">&lt;/filter-mapping&gt;</span></span>
</pre>
			<pre id="nav-hidden" class="nav-hidden">
</pre>
			<pre class="nav-text">
&lt;dispatcher&gt;可以配置0到4个，参数分别为REQUEST、INCLUDE、FORWARD、ERROR。
REQUEST：拦截用户的请求；
INCLUDE：拦截RequestDispatcher.include()方法调用的资源；
FORWARD：拦截RequestDispatcher.forward()方法调用的资源；
ERROR：拦截声明式异常处理机制调用的资源。
</pre>
		</div>
	</div>
</body>
</html>
